package com.one.blocks.mvc.convert;

import com.one.blocks.mvc.domain.BaseDomain;
import com.one.blocks.mvc.domain.Id;
import com.one.blocks.mvc.excel.ExcelConvert;
import org.mapstruct.BeanMapping;
import org.mapstruct.InheritConfiguration;
import org.mapstruct.Mapping;
import org.mapstruct.MappingTarget;
import org.mapstruct.Named;
import org.mapstruct.NullValueCheckStrategy;
import org.mapstruct.NullValuePropertyMappingStrategy;

import java.io.Serializable;

/**
 * @author <a href="mailto:idler41@163.con">linfuxin</a> created on 2024-01-11 11:27:00
 */
public interface BaseDomainExcelConvert<T extends BaseDomain<? extends Serializable> & Id<? extends Serializable>, E> extends ExcelConvert<T, E> {
    /**
     * excel转entity
     *
     * @param e excel对象
     * @return 实体对象
     */
    @Mapping(target = Id.FIELD_NAME_ID, ignore = true)
    @Mapping(target = BaseDomain.FIELD_NAME_CREATE_ID, expression = "java(SubjectHelper.getSubjectId())")
    @Mapping(target = BaseDomain.FIELD_NAME_CREATE_TIME, expression = "java(SubjectHelper.getAccessTime())")
    @Mapping(target = BaseDomain.FIELD_NAME_UPDATE_ID, ignore = true)
    @Mapping(target = BaseDomain.FIELD_NAME_UPDATE_TIME, ignore = true)
    @Override
    T excelToEntity(E e);

    /**
     * excel转entity(用以覆盖更新)
     *
     * @param t 实体对象
     * @param e excel对象
     */
    @Named("excelToEntityOverride")
    @InheritConfiguration(name = "excelToEntity")
    @BeanMapping(nullValuePropertyMappingStrategy = NullValuePropertyMappingStrategy.IGNORE, nullValueCheckStrategy = NullValueCheckStrategy.ALWAYS)
    @Mapping(target = BaseDomain.FIELD_NAME_CREATE_ID, ignore = true)
    @Mapping(target = BaseDomain.FIELD_NAME_CREATE_TIME, ignore = true)
    @Mapping(target = BaseDomain.FIELD_NAME_UPDATE_ID, expression = "java(SubjectHelper.getSubjectId())")
    @Mapping(target = BaseDomain.FIELD_NAME_UPDATE_TIME, expression = "java(SubjectHelper.getAccessTime())")
    @Override
    void excelToEntityOverride(E e, @MappingTarget T t);
}
